#pragma once

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
#include <QVector>
#include <QSqlDatabase>

class DatabaseTask : public QObject {
    Q_OBJECT
public:
    virtual ~DatabaseTask() {}
    virtual void execute() = 0;
};

// ECG数据插入任务
class EcgInsertTask : public DatabaseTask {
public:
    EcgInsertTask(const QVector<double>& data, qint64 timestamp, int subjectId)
        : m_data(data), m_timestamp(timestamp), m_subjectId(subjectId) {}

    void execute() override;

private:
    QVector<double> m_data;
    qint64 m_timestamp;
    int m_subjectId;
};

// EEG数据插入任务
class EegInsertTask : public DatabaseTask {
public:
    EegInsertTask(const QVector<QVector<double>>& processedData, qint64 timestamp, int subjectId)
        : m_processedData(processedData), m_timestamp(timestamp), m_subjectId(subjectId) {}

    void execute() override;

private:
    QVector<QVector<double>> m_processedData;
    qint64 m_timestamp;
    int m_subjectId;
};

// 数据库工作线程
class DatabaseWorker : public QObject {
    Q_OBJECT
public:
    explicit DatabaseWorker(QObject *parent = nullptr);
    ~DatabaseWorker();

    void addTask(DatabaseTask* task);
    void stop();

public slots:
    void process();

signals:
    void finished();

private:
    QMutex m_mutex;
    QWaitCondition m_condition;
    QQueue<DatabaseTask*> m_tasks;
    bool m_abort;
    QSqlDatabase m_db;
};
